package com.feicheng.lanniao.controller;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.feicheng.lanniao.entity.Car;
import com.feicheng.lanniao.entity.CarRentalInfo;
import com.feicheng.lanniao.entity.CarRentalOwner;
import com.feicheng.lanniao.entity.CarSellClientVo;
import com.feicheng.lanniao.entity.CarSellInfo;
import com.feicheng.lanniao.entity.CarSellOwner;
import com.feicheng.lanniao.exception.SystemException;
import com.feicheng.lanniao.result.ResultInfo;
import com.feicheng.lanniao.service.ICarSellClientVoService;
import com.feicheng.lanniao.service.ICarSellInfoService;
import com.feicheng.lanniao.service.ICarSellOwnerService;
import com.feicheng.lanniao.service.ICarService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author feichengTech
 * @since 2021-09-02
 */
@RestController
@RequestMapping("/lanniao-admin/car-sell-info")
public class CarSellInfoController {
	@Autowired
	ICarSellInfoService iCarSellInfoService;
	@Autowired
	ICarSellClientVoService iCarSellClientVoService;
	@Autowired
	ICarService iCarService;
	@Autowired
	ICarSellOwnerService iCarSellOwnerService;
	/**
	 * 查询所有卖车信息，并做分页
	 * @param searchKey 模糊查询参数
	 * @param clientId 用户ID
	 * @param conditionId 车龄ID
	 * @param mileageId 表显里程ID
	 * @param brandId 品牌ID
	 * @param sellPriceId 卖车价格ID
	 * @param currentPage 当前页
	 * @param pageSize 每页卖车信息条数
	 * @return 卖车信息集合
	 */
	@GetMapping("/page")
	public ResultInfo carSellList(
			String searchKey,
			Integer clientId ,
			Integer conditionId,
			Integer mileageId,
			Integer brandId,
			Integer sellPriceId,
			String typeName,
			@RequestParam(required = false,defaultValue = "1") Integer currentPage,
			@RequestParam(required = false,defaultValue = "10") Integer pageSize) {
		return iCarSellClientVoService.getCarSellClientVoPage(
				searchKey , clientId, conditionId, mileageId,
				brandId, sellPriceId,typeName,currentPage, pageSize);
	}
	/**
	 * 查询一条卖车信息
	 * @param carSellInfoId 通过主键查询
	 * @return 1条卖车信息对象
	 */
	@GetMapping("/info")
	public ResultInfo oneCarSellClientVo(Integer carSellInfoId) {
		return iCarSellClientVoService.getCarSellClientVoById(carSellInfoId);
	}
	/**
	 * 删除卖车信息
	 * @param carSellInfoId 通过主键删除1条卖车信息
	 * @return boolean值
	 */
	@DeleteMapping("/info")
	public ResultInfo deleteCarSellClientInfo(Integer carSellInfoId) {
		return iCarSellInfoService.deleteCarSellInfoById(carSellInfoId);
	}
	/**
	 * 修改卖车信息
	 * @param carSellClientVo 对象
	 * @return boolean值
	 */
	@PutMapping("/info")
	public ResultInfo updateCarSellInfo(CarSellClientVo carSellClientVo) {
		return iCarSellInfoService.updateCarSellInfo(carSellClientVo);
	}
	/**
	 * 增加卖车信息
	 * @param name 车名
	 * @param carCondition 车龄
	 * @param mileage 表显里程
	 * @param displacement 汽车排量
	 * @param transmissionCase 变速箱
	 * @param ownerShip 车牌归属地
	 * @param color 颜色
	 * @param productionDate 生产日期
	 * @param brandId 品牌ID
	 * @param type 车型
	 * @param standard 排放标准
	 * @param seats 座数
	 * @param energy 燃油类型
	 * @param drive 驱动
	 * @param imgUrl 图片地址
	 * @param sellPrice 卖车价格
	 * @param country 产地
	 * @param clientId 用户ID
	 * @return boolean值
	 */
	@PostMapping("/info")
	@Transactional
	public ResultInfo addCarSellInfo(
			Integer carCondition, // not null 车龄
			Integer mileage,   //not null   里程
			String ownerShip,
			Integer brandId,   //not null
			String type,       // 车型 not null
			String imgUrl,
			Integer sellPrice,
			Integer clientId,   // not null
			String tel,
			String wxNumber
			) {
		Car car = new Car();
		car.setCarCondition(carCondition);
		car.setMileage(mileage);
		car.setOwnerShip(ownerShip);
		car.setBrandId(brandId);
		car.setType(type);
		car.setImgUrl(imgUrl);
		car.setSellPrice(sellPrice);
		iCarService.save(car);

		//创建CarSellInfo对象
		CarSellInfo carSellInfo = new CarSellInfo();
		carSellInfo.setCarId(car.getCarId());
		carSellInfo.setSellPrice(car.getSellPrice());
		carSellInfo.setTel(tel);
		carSellInfo.setWxNumber(wxNumber);
		iCarSellInfoService.save(carSellInfo);
		//创建中间表对象
		CarSellOwner carSellOwner = new CarSellOwner();
		if(clientId == null){
			throw new SystemException("clientId不能为空");
		}
		carSellOwner.setClientId(clientId);
		carSellOwner.setCarSellInfoId(carSellInfo.getCarSellInfoId());
		iCarSellOwnerService.save(carSellOwner);
		return ResultInfo.success();
	}
}
